﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using Maticsoft.DBUtility;
using System.IO;

namespace SkWeb.RiskDB
{
    public partial class ImgDisp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strId = Request.QueryString["id"];
            string strScale = Request.QueryString["scale"];
            string strType  = Request.QueryString["map_type"];
            string strJh = Request.QueryString["jh"];
            //WebClient client = new WebClient();
             //client.DownloadFile("http://www.baidu.com/img/final_270_862e5f6d8d82c88d4d2f194c1acb803b.gif", ("c:/test1.jpg"));
            if (String.IsNullOrEmpty(strScale))
            {
                strScale = "12";
            }
            if(String.IsNullOrEmpty(strId))
            {
                return;
            }
            string strOverlayer = GetOverlayer(strId,strJh);
           
            string fileName = DateTime.Now.Ticks.ToString() + ".jpg";
            string path = Server.MapPath("~/tempImg/") + fileName;

            //Response.Write("http://api.map.baidu.com/staticimage?&width=400&height=380&zoom=" + strScale + strOverlayer);
            //client.DownloadFile("http://www.baidu.com/img/final_270_862e5f6d8d82c88d4d2f194c1acb803b.gif", Server.MapPath("~/tempImg1/" + DateTime.Now.Ticks.ToString() + ".jpg"));
           // client.DownloadFile("http://api.map.baidu.com/staticimage?&width=400&height=380&zoom=" + strScale  + strOverlayer, path);
         
           // SaveFileFromUrl(path, "http://api.map.baidu.com/staticimage?&width=400&height=380&zoom=" + strScale + strOverlayer);
           //long res= SaveFileFromUrl(path, "http://api.map.baidu.com/staticimage?&width=400&height=380&zoom=" + strScale + strOverlayer);
          // Response.Write(res);


            //FileStream fs = new FileStream(path, FileMode.Create);
            //StreamWriter mySw = new StreamWriter(fs);
            //mySw.Write("sdf");
            //mySw.Close();
            //fs.Close();

            this.Image1.ImageUrl = "http://api.map.baidu.com/staticimage?&width=400&height=380&zoom=" + strScale + strOverlayer;
        }
        //%257C
        private string  GetOverlayer(string strId,string strJh)
        {
            string strSql = "select name,lng,lat,rank,block_id from tb_wellinfo where id="+strId;
            DataTable dt = DbHelperOra.Query(strSql).Tables[0];
            DataRow dr = dt.Rows[0];
            
            //string str = "&markers=icon:" + "http://chart.apis.google.com/chart?chst=d_bubble_text_small%26chld=bb%257C"+strJh+"%257CFFFF88%257C000000" + "|" + dr["lat"].ToString() + "," + dr["lng"].ToString();// +"&markerStyles=l," + dr["rank"].ToString() + ",0xff0000";
            string str = "&center=" + dr["lng"].ToString() + "," + dr["lat"].ToString();
            string center_lng = dr["lng"].ToString();
            string center_lat = dr["lat"].ToString();

            string strBlockId = dr["block_id"].ToString();
            string strRank = dr["rank"].ToString();
            if (!String.IsNullOrEmpty(strBlockId) && Convert.ToInt32(strBlockId)>=0)
            {
                strSql = "select data,type from tb_riskblockinfo where id=" + strBlockId;
                dt = DbHelperOra.Query(strSql).Tables[0];
                if (dt.Rows.Count > 0)
                    dr = dt.Rows[0];
                else
                    return str;
                string strData = dr["data"].ToString();
                string[] temArr = strData.Split(',');//&language=zh-CN&path=weight:3|fillcolor:yellow:
                strData = "&paths=";//weight:3|fillcolor:";
                string strColor = "0xff0000";// "red";
                if (strRank == "2")
                {
                    strColor = "0xFFCC00";//"orange";
                }
                else if (strRank == "3")
                {
                    strColor = "0xffff00";// "yellow";//0xffff00
                }
                //strData += strColor+"|";

                //Array.Reverse(temArr);
                for (int i = 0; i < temArr.Length; i++)
                {
                    char ch = ',';
                    if (i % 2 == 1)
                    {
                        ch = ';';
                    }
                    strData += temArr[i] + ch;
                }
                strData += temArr[0] + ',' + temArr[1] ;               
                str = str+strData;

                str += " &pathStyles=0xff0000,1,0.5,"+strColor;
              
            }
            str += "&labels=" + "1" + "|" + center_lng + "," + center_lat;
            str += "&labelStyles=" + strJh + ",1,14,0x990099,0xff00,1";
         


            
            return str;
        }

        /// <summary>
        /// 从URL地址下载文件到本地磁盘
        /// </summary>
        /// <param name="ToLocalPath">本地磁盘地址</param>
        /// <param name="Url">URL网址</param>
        /// <returns></returns>
        public static long SaveFileFromUrl(string FileName, string Url,HttpResponse res)
        {
            long Value = 0;

            WebResponse response = null;
            Stream stream = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

                response = request.GetResponse();
                stream = response.GetResponseStream();

                if (!response.ContentType.ToLower().StartsWith("text/"))
                {
                    //SaveBinaryFile(response, FileName);

                    Value = response.ContentLength;

                }

            }
            catch (Exception err)
            {
                res.Write(err.Message);
                Value = 0;
                string aa = err.ToString();
            }
            return Value;
        }
        /// <summary>
        /// Save a binary file to disk.
        /// </summary>
        /// <param name="response">The response used to save the file</param>
        // 将二进制文件保存到磁盘
        private static bool SaveBinaryFile(WebResponse response, string FileName,HttpResponse res)
        {
            bool Value = true;
            byte[] buffer = new byte[1024];

            try
            {
                if (File.Exists(FileName))
                    File.Delete(FileName);
                Stream outStream = System.IO.File.Create(FileName);
                Stream inStream = response.GetResponseStream();

                int l;
                do
                {
                    l = inStream.Read(buffer, 0, buffer.Length);
                    if (l > 0)
                        outStream.Write(buffer, 0, l);
                }
                while (l > 0);

                outStream.Close();
                inStream.Close();
            }
            catch (Exception ex)
            {
                res.Write("1:"+ex.Message);
                Value = false;
            }
            return Value;
        }
























    }





    /*
     * google maps 
     * 
     * 
     *  public partial class ImgDisp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strId = Request.QueryString["id"];
            string strScale = Request.QueryString["scale"];
            string strType  = Request.QueryString["map_type"];
            string strJh = Request.QueryString["jh"];
            if (String.IsNullOrEmpty(strScale))
            {
                strScale = "12";
            }
            if(String.IsNullOrEmpty(strId))
            {
                return;
            }
            string strOverlayer = GetOverlayer(strId,strJh);
            WebClient client = new WebClient();
            string fileName = DateTime.Now.Ticks.ToString() + ".jpg";
            string path = Server.MapPath("~/tempImg/") + fileName;
            Response.Write("http://maps.google.com/maps/api/staticmap?&size=400x380&sensor=false&language=zh-CN&zoom=" + strScale + "&maptype=" + strType + strOverlayer);
            string strIcon = "https://chart.googleapis.com/chart?chst=d_bubble_icon_text_small&chld=ski|bb|Wheeee!|FFFFFF|000000";
           // client.DownloadFile("http://maps.google.com/maps/api/staticmap?&size=400x380&sensor=false&language=zh-CN&zoom=" + strScale +"&maptype="+strType+ strOverlayer, path);
           // Response.Write("http://maps.google.com/maps/api/staticmap?&size=400x380&sensor=false&language=zh-CN&zoom=" + strScale + "&maptype=" + strType + strOverlayer);
            this.Image1.ImageUrl = "http://maps.google.com/maps/api/staticmap?&size=400x380&sensor=false&language=zh-CN&zoom=" + strScale + "&maptype=" + strType + strOverlayer;//"../tempImg/" + fileName;
        }
        //%257C
        private string  GetOverlayer(string strId,string strJh)
        {
            string strSql = "select name,lng,lat,rank,block_id from tb_wellinfo where id="+strId;
            DataTable dt = DbHelperOra.Query(strSql).Tables[0];
            DataRow dr = dt.Rows[0];
            string str = "&markers=icon:" + "http://chart.apis.google.com/chart?chst=d_bubble_text_small%26chld=bb%257C"+strJh+"%257CFFFF88%257C000000" + "|" + dr["lat"].ToString() + "," + dr["lng"].ToString();// +"&markerStyles=l," + dr["rank"].ToString() + ",0xff0000";
           
            string strBlockId = dr["block_id"].ToString();
            string strRank = dr["rank"].ToString();
            if (!String.IsNullOrEmpty(strBlockId))
            {
                strSql = "select data,type from tb_riskblockinfo where id=" + strBlockId;
                dt = DbHelperOra.Query(strSql).Tables[0];
                if (dt.Rows.Count > 0)
                    dr = dt.Rows[0];
                else
                    return str;
                string strData = dr["data"].ToString();
                string[] temArr = strData.Split(',');//&language=zh-CN&path=weight:3|fillcolor:yellow:
                strData = "&path=weight:3|fillcolor:";
                string strColor = "red";
                if (strRank == "2")
                {
                    strColor = "orange";
                }
                else if (strRank == "3")
                {
                    strColor = "yellow";
                }
                strData += strColor+"|";

                Array.Reverse(temArr);
                for (int i = 0; i < temArr.Length; i++)
                {
                    char ch = ',';
                    if (i % 2 == 1)
                    {
                        ch = '|';
                    }
                    strData += temArr[i] + ch;
                }
                strData += temArr[0] + ',' + temArr[1] ;               
                str = str+strData;
            }
            
            return str;
        }
    }
     * 
     * 
     * 
     * 
     * 
     * 
     * */






}